const utils = require('../../utils');

module.exports = function ({ uri = 'mongodb://localhost:27017', database, table, cond, sort, count=1 }) {
  const mongodb = require("mongodb");
  return new Promise((resolve, reject) => {
    const MongoClient = mongodb.MongoClient;
    MongoClient.connect(uri, { socketTimeoutMS: 10000000 }, function (err, db) {
      if (err) return reject(err);
      utils.debug("mongodb已连接");
      const st = db.db(database).collection(table).find(cond).sort(sort).limit(1).stream();
      st.on("end", () => {
        db.close();
        utils.debug('mongodbReader [end]');
      });
      return resolve(st);
    });
  });
};
